// pages/login/login.js
var app=getApp()
Page({

  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide:true
},

onLoad: function() {
    var that = this;
    // 查看是否授权
    wx.getSetting({
        success: function(res) {
            if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({
                    success: function(res) {
                        wx.login({
                            success: res => {
                                console.log("用户的code:" + res.code);
                                wx.request({
                                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid="wx4a3de1f3cfe49f7f"&secret="30767a7a7ca6cccb42f3b64ed103f55b"&js_code="011qa1000ObJOM1GaP000IoOHa1qa10-"' + res.code + '&grant_type=authorization_code',
                                    success: res => {
                                        console.log("用户的openid:" + res.data.openid);
                                    }
                                });
                            },
                        });
                    }
                });
            } else {
                
                that.setData({
                    isHide: false
                });
            }
        }
    });
},



bindGetUserInfo: function(e) {
    if (e.detail.userInfo) {
        let that = this;
        wx.request({
            url: app.globalData.url+'/c',
            method: 'POST',
            data: {
                user:e.detail.userInfo.nickName,
                img:e.detail.userInfo.avatarUrl
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
                app.globalData.user_head_imgPath=e.detail.userInfo.avatarUrl
                app.globalData.user_head_name=e.detail.userInfo.nickName
            }
            
        })
        console.log(e.detail.userInfo);
        getApp().globalData.userInfo=e.detail.userInfo
        console.log(app.globalData.userInfo)
        that.setData({
            isHide: true
        });
        wx.switchTab({
          url: '/pages/home/home',
        })
    } else {
        wx.showModal({
            title: '警告',
            content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
            showCancel: false,
            confirmText: '返回授权',
            success: function(res) {
                if (res.confirm) {
                    console.log('用户点击了“返回授权”');
                }
            }
        });
    }
},
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})